<?php
/**
 * @copyright Copyright (C) 2010 redCOMPONENT.com. All rights reserved.
 * @license GNU/GPL, see license.txt or http://www.gnu.org/copyleft/gpl.html
 * Developed by email@recomponent.com - redCOMPONENT.com
 *
 * redSHOP can be downloaded from www.redcomponent.com
 * redSHOP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with redSHOP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

require_once( JPATH_SITE.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'product.php');
require_once( JPATH_SITE.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'helper.php');
require_once( JPATH_SITE.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'user.php');
require_once( JPATH_SITE.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'cart.php');
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'extra_field.php' );
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'mail.php' );
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'order.php' );
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'product.php' );
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_redshop'.DS.'helpers'.DS.'shipping.php' );

class addorder_detailModeladdorder_detail extends JModel
{
	var $_id = null;
	var $_data = null;
	var $_table_prefix = null;
	var $_copydata	=	null;

	function __construct()
	{
		parent::__construct();
		$this->_table_prefix = '#__redshop_';
		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
		$this->_order_functions	= new order_functions();
		$this->_db = & JFactory::getDBO();
	}
	function setId($id)
	{
		$this->_id		= $id;
		$this->_data	= null;
	}

	function &getData()
	{
		if ($this->_loadData())
		{

		}else  $this->_initData();

	   	return $this->_data;
	}

	function _loadData()
	{
		$order_functions = new order_functions();
		if (empty($this->_data))
		{
			$this->_data = $order_functions->getOrderDetails($this->_id);
			return (boolean) $this->_data;
		}

		return true;
	}

	function setBilling()
	{
		$post = JRequest::get ( 'post' );
		
		$is_company = (DEFAULT_CUSTOMER_REGISTER_TYPE==2) ? 1 :0;
		$detail = new stdClass();
		$detail->users_info_id			= (isset($post['users_info_id'])) ? $post['users_info_id'] : 0;
		$detail->address_type			= (isset($post['address_type'])) ? $post['address_type'] : "";
		$detail->company_name			= (isset($post['company_name'])) ? $post['company_name'] : null;
		$detail->firstname		 		= (isset($post['firstname'])) ? $post['firstname'] : null;
		$detail->lastname				= (isset($post['lastname'])) ? $post['lastname'] : null;
		$detail->country_code			= (isset($post['country_code'])) ? $post['country_code'] : null;
		$detail->state_code				= (isset($post['state_code'])) ? $post['state_code'] : null;
		$detail->zipcode				= (isset($post['zipcode'])) ? $post['zipcode'] : null;
		$detail->user_email				= (isset($post['user_email'])) ? $post['user_email'] : null;
		$detail->address				= (isset($post['address'])) ? $post['address'] : null;
		$detail->is_company				= (isset($post['is_company'])) ? $post['is_company'] : $is_company;
		$detail->city					= (isset($post['city'])) ? $post['city'] : null;
		$detail->phone					= (isset($post['phone'])) ? $post['phone'] : null;
		$detail->vat_number				= (isset($post['vat_number'])) ? $post['vat_number'] : null;
		$detail->tax_exempt_approved	= (isset($post['tax_exempt_approved'])) ? $post['tax_exempt_approved'] : null;
		$detail->requesting_tax_exempt	= (isset($post['requesting_tax_exempt'])) ? $post['requesting_tax_exempt'] : null;
		$detail->ean_number				= (isset($post['ean_number'])) ? $post['ean_number'] : null;
		$detail->tax_exempt				= (isset($post['tax_exempt'])) ? $post['tax_exempt'] : null;
		return $detail;
	}
	
	function setShipping()
	{
		$post = JRequest::get ( 'post' );
				
		$detail = new stdClass();
		$detail->billisship				= (isset($post['billisship'])) ? $post['billisship'] : 1;
		$detail->users_info_id			= (isset($post['users_info_id'])) ? $post['users_info_id'] : 0;
		$detail->firstname		 		= (isset($post['firstname_ST'])) ? $post['firstname_ST'] : null;
		$detail->lastname				= (isset($post['lastname_ST'])) ? $post['lastname_ST'] : null;
		$detail->country_code			= (isset($post['country_code_ST'])) ? $post['country_code_ST'] : null;
		$detail->state_code				= (isset($post['state_code_ST'])) ? $post['state_code_ST'] : null;
		$detail->zipcode				= (isset($post['zipcode_ST'])) ? $post['zipcode_ST'] : null;
		$detail->address				= (isset($post['address_ST'])) ? $post['address_ST'] : null;
		$detail->city					= (isset($post['city_ST'])) ? $post['city_ST'] : null;
		$detail->phone					= (isset($post['phone_ST'])) ? $post['phone_ST'] : null;
		
		return $detail;
	}
	
	function _initData()
	{
		if (empty($this->_data))
		{
			$detail = new stdClass();
			$detail->order_id				= 0;
			$detail->user_id				= null;
			$detail->order_number			= null;
			$detail->user_info_id	 		= null;
			$detail->order_total			= null;
			$detail->order_subtotal			= null;
			$detail->order_tax				= null;
			$detail->order_tax_details		= null;
			$detail->order_shipping			= null;
			$detail->order_shipping_tax		= null;
			$detail->coupon_discount		= null;
			$detail->payment_discount       = null;
			$detail->order_discount			= null;
			$detail->order_status			= null;
			$detail->cdate					= null;
			$detail->mdate					= null;
			$detail->ship_method_id			= null;
			$detail->customer_note			= null;
			$detail->ip_address				= null;
			$this->_data		 			= $detail;
			return (boolean) $this->_data;
		}
		return true;
	}

	function storeShipping($data)
	{
		$userhelper = new rsUserhelper();
		$data['address_type'] 	= 'BT';
		$data['createaccount'] = (isset($data['username']) && $data['username']!="") ? 1 : 0;
	   	$data['user_email'] = $data['email1'] = $data['email'];
	   	$data['sameasbilling'] = (isset($data['billisship']) && $data['billisship']==1) ? 1 : 0;
	   	$data['billisship'] = 1;
	   	$data['groups'] = array("Registered" => "2");
	   	if($data['guestuser'] && !$data['user_id'])
		{
			$joomlauser = $userhelper->updateJoomlaUser($data);
			if(!$joomlauser)
			{
				return false;
			}
		}
		$reduser = $userhelper->storeRedshopUser($data,$joomlauser->id,1);
		if($reduser)
		{
			if($data['sameasbilling']!=1)
			{
				$data['users_info_id'] = ($data['shipp_users_info_id']!="") ? $data['shipp_users_info_id'] : 0;
				$data['user_email'] = $reduser->user_email;
				$data['user_id'] = $reduser->user_id;
				$data['tax_exempt'] = $reduser->tax_exempt;
				$data['requesting_tax_exempt'] = $reduser->requesting_tax_exempt;
				$data['shopper_group_id'] = $reduser->shopper_group_id;
				$data['tax_exempt_approved'] = $reduser->tax_exempt_approved;
				$data['company_name'] = $reduser->company_name;
				$data['vat_number'] = $reduser->vat_number;
				if($data['firstname_ST']=="")
				{
					$data['firstname_ST'] = $data['firstname'];
				}
				if($data['lastname_ST']=="")
				{
					$data['lastname_ST'] = $data['lastname'];
				}
				if($data['address_ST']=="")
				{
					$data['address_ST'] = $data['address'];
				}
				if($data['city_ST']=="")
				{
					$data['city_ST'] = $data['city'];
				}
				if($data['state_code_ST']=="0")
				{
					$data['state_code_ST'] = $data['state_code'];
				}
				if($data['country_code_ST']=="0")
				{
					$data['country_code_ST'] = $data['country_code'];
				}
				if($data['zipcode_ST']=="")
				{
					$data['zipcode_ST'] = $data['zipcode'];
				}
				if($data['phone_ST']=="")
				{
					$data['phone_ST'] = $data['phone'];
				}
		
				$rowsh = $userhelper->storeRedshopUserShipping($data);
				return $rowsh;
			}
			else
			{
				$reduser->users_info_id = 0;
				return $reduser;
			}
		}
		return $reduser;
	}

  	function store($postdata)
	{
		$redconfig = new Redconfiguration();
		$redshopMail = new redshopMail();
		$order_functions = new order_functions();
		$helper = new redhelper();
		$producthelper = new producthelper();
		$rsCarthelper = new rsCarthelper();
		$shippinghelper = new shipping();
		$objshipping = new shipping();
		$adminproducthelper = new adminproducthelper();
		$stockroomhelper = new rsstockroomhelper();

		// for barcode generation
 		$barcode_code=$order_functions->barcode_randon_number(12,0);
		// end
		$postdata['barcode'] = $barcode_code;

		$row =& $this->getTable('order_detail');
		if (!$row->bind($postdata)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		$iscrm = $helper->isredCRM();
		if($iscrm)
		{
			$postdata['order_id'] = $row->order_id;
			$postdata['debitor_id'] = $postdata['user_info_id'];
			JTable :: addIncludePath(REDCRM_ADMIN.DS.'tables');

			$crmorder = & $this->getTable ( 'crm_order' );

			if (! $crmorder->bind($postdata)) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			if (! $crmorder->store()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}

			# update rma table entry
			if(ENABLE_RMA && isset($postdata['rmanotes'])){

				$rmaInfo =& $this->getTable('rma_orders');

				$rmaInfo->rma_number = $postdata['rma_number'];
				$rmaInfo->original_order_id = $postdata['main_order_id'];
				$rmaInfo->credit_note_order_id = $row->order_id;
				$rmaInfo->rma_note = $postdata['rmanotes'];
				$rmaInfo->store();
			}
			# END

			JTable::addIncludePath(REDSHOP_ADMIN.DS.'tables');
		}
		$order_shipping = explode ( "|", $shippinghelper->decryptShipping(str_replace(" ","+",$row->ship_method_id) ));

		$rowOrderStatus = & $this->getTable ( 'order_status_log' );
		$rowOrderStatus->order_id = $row->order_id;
		$rowOrderStatus->order_status = $row->order_status;
		$rowOrderStatus->date_changed = time();
		$rowOrderStatus->customer_note = $row->customer_note;
		$rowOrderStatus->store();

		$billingaddresses = $order_functions->getBillingAddress($row->user_id);
		if(isset($postdata['billisship']) && $postdata['billisship'] == 1)
		{
			$shippingaddresses = $billingaddresses;
		}
		else
		{
			$key = 0;
			$shippingaddresses = $order_functions->getShippingAddress($row->user_id);
			$shipp_users_info_id = (isset($postdata['shipp_users_info_id']) && $postdata['shipp_users_info_id']!=0) ? $postdata['shipp_users_info_id'] : 0;
			if($shipp_users_info_id!=0)
			{
				for($o=0;$o<count($shippingaddresses);$o++)
				{
					if($shippingaddresses[$o]->users_info_id == $shipp_users_info_id)
					{
						$key=$o;
						break;
					}
				}
			}
			$shippingaddresses = $shippingaddresses[$key];
		}

		//ORDER DELIVERY TIME IS REMAINING

		$user_id = $row->user_id;
		$item = $postdata['order_item'];
		for($i=0;$i<count($item);$i++)
		{
			$product_id = $item[$i]->product_id;
			$quantity = $item[$i]->quantity;
			$product_excl_price = $item[$i]->prdexclprice;
			$product_price = $item[$i]->productprice;

			///////////// Attribute price added ///////////////////////
			$generateAttributeCart = $rsCarthelper->generateAttributeArray((array)$item[$i],$user_id);
			$retAttArr = $producthelper->makeAttributeCart($generateAttributeCart,$product_id,$user_id,0,$quantity);
			$product_attribute = $retAttArr[0];

			////////////// Accessory price /////////////
			$generateAccessoryCart = $rsCarthelper->generateAccessoryArray((array)$item[$i],$user_id);
			$retAccArr = $producthelper->makeAccessoryCart($generateAccessoryCart,$product_id,$user_id);
			$product_accessory = $retAccArr[0];
			$accessory_total_price = $retAccArr[1];
			$accessory_vat_price = $retAccArr[2];

			$wrapper_price = 0;
			$wrapper_vat = 0;
			if($item[$i]->wrapper_data!=0 && $item[$i]->wrapper_data!='')
			{
				$wrapper = $producthelper->getWrapper($product_id,$item[$i]->wrapper_data);
				if(count($wrapper)>0)
				{
					if($wrapper[0]->wrapper_price > 0 ) {
						$wrapper_vat = $producthelper->getProducttax($product_id,$wrapper[0]->wrapper_price,$user_id);
					}
					$wrapper_price = $wrapper[0]->wrapper_price + $wrapper_vat;
				}
			}
			$product = $producthelper->getProductById ( $product_id );

			$rowitem = & $this->getTable ( 'order_item_detail' );
			if (! $rowitem->bind ( $postdata )) {
				$this->setError ( $this->_db->getErrorMsg () );
				return false;
			}
			//	STOCKROOM update
			$updatestock = $stockroomhelper->updateStockroomQuantity($product_id,$quantity);
			$stockroom_id_list  = $updatestock['stockroom_list'];
			$stockroom_quantity_list = $updatestock['stockroom_quantity_list'];
			$rowitem->stockroom_id 	= $stockroom_id_list;
			$rowitem->stockroom_quantity = $stockroom_quantity_list;
			$rowitem->order_item_id = 0;
			$rowitem->order_id = $row->order_id;
			$rowitem->user_info_id = $row->user_info_id;
			$rowitem->supplier_id = $product->manufacturer_id;
			$rowitem->product_id = $product_id;
			$rowitem->order_item_sku = $product->product_number;
			$rowitem->order_item_name = $product->product_name;
			$rowitem->product_quantity = $quantity;
			$rowitem->product_item_price = $product_price;
			$rowitem->product_item_price_excl_vat = $product_excl_price;
			$rowitem->product_final_price = $product_price * $quantity;
			$rowitem->order_item_currency = REDCURRENCY_SYMBOL;
			$rowitem->order_status = $row->order_status;
			$rowitem->cdate = $row->cdate;
			$rowitem->mdate = $row->cdate;
			$rowitem->product_attribute = $product_attribute;
			$rowitem->product_accessory = $product_accessory;
//			$rowitem->container_id = $objshipping->getProductContainerId ( $product_id );
			$rowitem->wrapper_id = $item[$i]->wrapper_data;
			$rowitem->wrapper_price = $wrapper_price;
			$rowitem->is_giftcard = 0;

			# redCRM product purchase price
			if($iscrm)
			{
				$crmProductHelper = new crmProductHelper();
				$crmproduct = $crmProductHelper->getProductById($product_id);

				$rowitem->product_purchase_price	= $crmproduct->product_purchase_price > 0 ? $crmproduct->product_purchase_price : $crmproduct->product_price ;
			}
			# End

			if($producthelper->checkProductDownload($product_id))
			{
				$medianame = $producthelper->getProductMediaName($product_id);
				for($j=0; $j<count($medianame); $j++)
				{
					$product_serial_number = $producthelper->getProdcutSerialNumber( $product_id );
					$producthelper->insertProductDownload($product_id,$user_id,$rowitem->order_id,$medianame[$j]->media_name,$product_serial_number->serial_number);
				}
			}
			if (! $rowitem->store ()) {
				$this->setError ( $this->_db->getErrorMsg () );
				return false;
			}

			/** my accessory save in table start */
			if(count($generateAccessoryCart)>0)
			{
				$attArr = $generateAccessoryCart;
				for($a=0;$a<count($attArr);$a++)
				{
					$accessory_vat_price = 0;
					$accessory_attribute = "";
					$accessory_id = $attArr[$a]['accessory_id'];
					$accessory_name = $attArr[$a]['accessory_name'];
					$accessory_price = $attArr[$a]['accessory_price'];
					$accessory_org_price = $accessory_price;
					if($accessory_price>0)
					{
						$accessory_vat_price = $producthelper->getProductTax($product_id,$accessory_price,$user_id);
					}
					$attchildArr = $attArr[$a]['accessory_childs'];
					for($j=0;$j<count($attchildArr);$j++)
					{
						$attribute_id = $attchildArr[$j]['attribute_id'];
						$accessory_attribute .= urldecode($attchildArr[$j]['attribute_name']).":<br/>";

						$rowattitem = & $this->getTable ( 'order_attribute_item' );
						$rowattitem->order_att_item_id = 0;
						$rowattitem->order_item_id = $rowitem->order_item_id;
						$rowattitem->section_id = $attribute_id;
						$rowattitem->section = "attribute";
						$rowattitem->parent_section_id = $accessory_id;
						$rowattitem->section_name = $attchildArr[$j]['attribute_name'];
						$rowattitem->is_accessory_att = 1;
						if($attribute_id>0)
						{
							if(!$rowattitem->store())
							{
								$this->setError ( $this->_db->getErrorMsg () );
								return false;
							}
						}

						$propArr = $attchildArr[$j]['attribute_childs'];
						for($k=0;$k<count($propArr);$k++)
						{
							$section_vat = 0;
							if($propArr[$k]['property_price']>0)
							{
								$section_vat = $producthelper->getProducttax($product_id,$propArr[$k]['property_price'],$user_id);
							}
							$property_id = $propArr[$k]['property_id'];
							$accessory_attribute .= urldecode($propArr[$k]['property_name'])." (".$propArr[$k]['property_oprand'].$producthelper->getProductFormattedPrice($propArr[$k]['property_price'] + $section_vat).")<br/>";
							$subpropArr = $propArr[$k]['property_childs'];

							$rowattitem = & $this->getTable ( 'order_attribute_item' );
							$rowattitem->order_att_item_id = 0;
							$rowattitem->order_item_id = $rowitem->order_item_id;
							$rowattitem->section_id = $property_id;
							$rowattitem->section = "property";
							$rowattitem->parent_section_id = $attribute_id;
							$rowattitem->section_name = $propArr[$k]['property_name'];
							$rowattitem->section_price = $propArr[$k]['property_price'];
							$rowattitem->section_vat = $section_vat;
							$rowattitem->section_oprand = $propArr[$k]['property_oprand'];
							$rowattitem->is_accessory_att = 1;
							if($property_id>0)
							{
								if(!$rowattitem->store())
								{
									$this->setError ( $this->_db->getErrorMsg () );
									return false;
								}
							}

							for($l=0;$l<count($subpropArr);$l++)
							{
								$section_vat = 0;
								if($subpropArr[$l]['subproperty_price']>0)
								{
									$section_vat = $producthelper->getProducttax($rowitem->product_id,$subpropArr[$l]['subproperty_price'],$user_id);
								}
								$subproperty_id = $subpropArr[$l]['subproperty_id'];
								$accessory_attribute .= urldecode($subpropArr[$l]['subproperty_name'])." (".$subpropArr[$l]['subproperty_oprand'].$producthelper->getProductFormattedPrice($subpropArr[$l]['subproperty_price'] + $section_vat).")<br/>";

								$rowattitem = & $this->getTable ( 'order_attribute_item' );
								$rowattitem->order_att_item_id = 0;
								$rowattitem->order_item_id = $rowitem->order_item_id;
								$rowattitem->section_id = $subproperty_id;
								$rowattitem->section = "subproperty";
								$rowattitem->parent_section_id = $property_id;
								$rowattitem->section_name = $subpropArr[$l]['subproperty_name'];
								$rowattitem->section_price = $subpropArr[$l]['subproperty_price'];
								$rowattitem->section_vat = $section_vat;
								$rowattitem->section_oprand = $subpropArr[$l]['subproperty_oprand'];
								$rowattitem->is_accessory_att = 1;
								if($subproperty_id>0)
								{
									if(!$rowattitem->store())
									{
										$this->setError ( $this->_db->getErrorMsg () );
										return false;
									}
								}
							}
						}
					}

					$accdata = & $this->getTable ( 'accessory_detail' );
					if($accessory_id>0)
					{
						$accdata->load($accessory_id);
					}
					$accProductinfo = $producthelper->getProductById($accdata->child_product_id);
					$rowaccitem = & $this->getTable ( 'order_acc_item' );
					$rowaccitem->order_item_acc_id = 0;
					$rowaccitem->order_item_id = $rowitem->order_item_id;
					$rowaccitem->product_id = $accessory_id;
					$rowaccitem->order_acc_item_sku = $accProductinfo->product_number;
					$rowaccitem->order_acc_item_name = $accessory_name;
					$rowaccitem->order_acc_price = $accessory_org_price;
					$rowaccitem->order_acc_vat = $accessory_vat_price;
					$rowaccitem->product_quantity = $quantity;
					$rowaccitem->product_acc_item_price = $accessory_price;
					$rowaccitem->product_acc_final_price = ($accessory_price * $quantity);
					$rowaccitem->product_attribute = $accessory_attribute;
					if($accessory_id>0)
					{
						if(!$rowaccitem->store())
						{
							$this->setError ( $this->_db->getErrorMsg () );
							return false;
						}
					}
				}
			}

			/** my attribute save in table start */
			if(count($generateAttributeCart)>0)
			{
				$attArr = $generateAttributeCart;
				for($j=0;$j<count($attArr);$j++)
				{
					$attribute_id = $attArr[$j]['attribute_id'];

					$rowattitem = & $this->getTable ( 'order_attribute_item' );
					$rowattitem->order_att_item_id = 0;
					$rowattitem->order_item_id = $rowitem->order_item_id;
					$rowattitem->section_id = $attribute_id;
					$rowattitem->section = "attribute";
					$rowattitem->parent_section_id = $rowitem->product_id;
					$rowattitem->section_name = $attArr[$j]['attribute_name'];
					$rowattitem->is_accessory_att = 0;
					if($attribute_id>0)
					{
						if(!$rowattitem->store())
						{
							$this->setError ( $this->_db->getErrorMsg () );
							return false;
						}
					}

					$propArr = $attArr[$j]['attribute_childs'];
					for($k=0;$k<count($propArr);$k++)
					{
						$section_vat = 0;
						if($propArr[$k]['property_price']>0)
						{
							$section_vat = $producthelper->getProducttax($rowitem->product_id,$propArr[$k]['property_price'],$user_id);
						}
						$property_id = $propArr[$k]['property_id'];
						/** product property STOCKROOM update start */
						$updatestock = $stockroomhelper->updateStockroomQuantity($property_id,$quantity,"property");

						$rowattitem = & $this->getTable ( 'order_attribute_item' );
						$rowattitem->order_att_item_id = 0;
						$rowattitem->order_item_id = $rowitem->order_item_id;
						$rowattitem->section_id = $property_id;
						$rowattitem->section = "property";
						$rowattitem->parent_section_id = $attribute_id;
						$rowattitem->section_name = $propArr[$k]['property_name'];
						$rowattitem->section_price = $propArr[$k]['property_price'];
						$rowattitem->section_vat = $section_vat;
						$rowattitem->section_oprand = $propArr[$k]['property_oprand'];
						$rowattitem->is_accessory_att = 0;
						if($property_id>0)
						{
							if(!$rowattitem->store())
							{
								$this->setError ( $this->_db->getErrorMsg () );
								return false;
							}
						}

						$subpropArr = $propArr[$k]['property_childs'];
						for($l=0;$l<count($subpropArr);$l++)
						{
							$section_vat = 0;
							if($subpropArr[$l]['subproperty_price']>0)
							{
								$section_vat = $producthelper->getProducttax($product_id,$subpropArr[$l]['subproperty_price'],$user_id);
							}
							$subproperty_id = $subpropArr[$l]['subproperty_id'];
							/** product subproperty STOCKROOM update start */
							$updatestock = $stockroomhelper->updateStockroomQuantity($subproperty_id,$quantity,"subproperty");

							$rowattitem = & $this->getTable ( 'order_attribute_item' );
							$rowattitem->order_att_item_id = 0;
							$rowattitem->order_item_id = $rowitem->order_item_id;
							$rowattitem->section_id = $subproperty_id;
							$rowattitem->section = "subproperty";
							$rowattitem->parent_section_id = $property_id;
							$rowattitem->section_name = $subpropArr[$l]['subproperty_name'];
							$rowattitem->section_price = $subpropArr[$l]['subproperty_price'];
							$rowattitem->section_vat = $section_vat;
							$rowattitem->section_oprand = $subpropArr[$l]['subproperty_oprand'];
							$rowattitem->is_accessory_att = 0;
							if($subproperty_id>0)
							{
								if(!$rowattitem->store())
								{
									$this->setError ( $this->_db->getErrorMsg () );
									return false;
								}
							}
						}
					}
				}
			}

//			$producthelper->insertProdcutUserfield($i,$item,$rowitem->order_item_id,12);
			if (USE_CONTAINER)
			{
				$producthelper->updateContainerStock($product_id,$quantity,$rowitem->container_id);
			}
			// store userfields
			$userfields = $item[$i]->extrafieldname;
			$userfields_id = $item[$i]->extrafieldId;
			for($ui=0;$ui<count($userfields);$ui++)
			{
				$adminproducthelper->admin_insertProdcutUserfield($userfields_id[$ui],$rowitem->order_item_id,12,$userfields[$ui]);
			}

			# redCRM RMA Transaction Entry
			if($iscrm)
			{
				if(ENABLE_RMA && $rowitem->product_final_price<0){

					# RMA transation log
					if(isset($item[$i]->reason)){

						$rmaTrans =& $this->getTable('rma_transaction');
						$rmaTrans->rma_transaction_id = 0;
						$rmaTrans->rma_number = $postdata['rma_number'];
						$rmaTrans->order_item_return_id = $rowitem->order_item_id;
						$rmaTrans->order_item_return_reason = $item[$i]->reason;
						$rmaTrans->order_item_return_status = $item[$i]->deposition;
						$rmaTrans->order_item_return_action = $item[$i]->action;
						$rmaTrans->cdate	=	time();
						$rmaTrans->store();

						if(ENABLE_ITEM_TRACKING_SYSTEM){
							# manage supplier order stock
							$crmSupplierOrderHelper = new crmSupplierOrderHelper();

							$senddata['main_order_number'] = $postdata['main_order_number'];
							$senddata['order_status'] = $row->order_status;
							$senddata['product_id'] = $rowitem->product_id;
							$senddata['property_id'] = $property_id;
							$senddata['subproperty_id'] = $subproperty_id;
							$senddata['deposition'] = $item[$i]->deposition;

							$itemqty = $rowitem->product_quantity;
							for($r=0;$r<$itemqty;$r++){
								$crmSupplierOrderHelper->manageStockAffectedRMA($senddata);
							}
						}
					}
				}

			}
			#End

		}

		$rowpayment = & $this->getTable ( 'order_payment' );
		if (! $rowpayment->bind ( $postdata )) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}
		$rowpayment->order_id 				= $row->order_id;
		$rowpayment->payment_method_id 		= $postdata['payment_method_class'];
		$rowpayment->order_payment_amount 	= $row->order_total;
		$rowpayment->order_payment_name 	= $postdata['order_payment_name'];
		$rowpayment->payment_method_class 	= $postdata['payment_method_class'];
		if (! $rowpayment->store ()) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}

		# add billing Info
		$userrow = & $this->getTable ( 'user_detail' );
		$userrow->load ( $billingaddresses->users_info_id );
		$orderuserrow = & $this->getTable ( 'order_user_detail' );
		if (! $orderuserrow->bind ( $userrow )) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}
		$orderuserrow->order_id = $row->order_id;
		$orderuserrow->address_type = 'BT';
		if (! $orderuserrow->store ()) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}

		# add shipping Info
		$userrow = & $this->getTable ( 'user_detail' );
		if(isset($shippingaddresses->users_info_id))
		{
			$userrow->load ( $shippingaddresses->users_info_id );
		}
		$orderuserrow = & $this->getTable ( 'order_user_detail' );
		if (! $orderuserrow->bind ( $userrow )) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}
		$orderuserrow->order_id = $row->order_id;
		$orderuserrow->address_type = 'ST';
		if (! $orderuserrow->store ()) {
			$this->setError ( $this->_db->getErrorMsg () );
			return false;
		}

		if($row->order_status == CLICKATELL_ORDER_STATUS)
		{
			$helper->clickatellSMS($row->order_id );
		}

		# maintan supplier order stck when item tracking system is enabled
		if($helper->isredCRM())
		{
			if(ENABLE_ITEM_TRACKING_SYSTEM){

				# Supplier order helper object
				$crmSupplierOrderHelper = new crmSupplierOrderHelper();

				$getStatus = array();
				$getStatus['orderstatus'] = $row->order_status;
				$getStatus['paymentstatus'] = $row->order_payment_status;

				$crmSupplierOrderHelper->redSHOPOrderUpdate($row->order_id,$getStatus);
				unset($getStatus);
			}
		}
		# End

		$checkOrderStatus = 1;
		if ($postdata['payment_method_class']== "rs_payment_banktransfer" || $postdata['payment_method_class']== "rs_payment_banktransfer_discount" || $postdata['payment_method_class'] == "rs_payment_banktransfer2" || $postdata['payment_method_class'] == "rs_payment_banktransfer3" || $postdata['payment_method_class'] == "rs_payment_banktransfer4" || $postdata['payment_method_class'] == "rs_payment_banktransfer5")
		{
			$checkOrderStatus = 0;
		}
		// Economic Integration start for invoice generate and book current invoice
		if(ECONOMIC_INTEGRATION==1 && ECONOMIC_INVOICE_DRAFT!=2)
		{
			$issplit = 0;
			$economic = new economic();
			if(isset($postdata['issplit']) && $postdata['issplit']==1)
			{
				$issplit = 1;
			}
			$economicdata['split_payment'] = $issplit;
			$economicdata['economic_payment_terms_id'] = $postdata['economic_payment_terms_id'];
			$economicdata['economic_design_layout'] = $postdata['economic_design_layout'];
			$economicdata['economic_is_creditcard'] = $postdata['economic_is_creditcard'];
			$payment_name = $postdata['payment_method_class'];
			$paymentArr = explode("rs_payment_",$postdata['payment_method_class']);
			if(count($paymentArr)>0)
			{
				$payment_name = $paymentArr[1];
			}
			$economicdata['economic_payment_method']= $payment_name;

			$invoiceHandle = $economic->createInvoiceInEconomic($row->order_id,$economicdata);

			if(ECONOMIC_INVOICE_DRAFT==0)
			{
				$bookinvoicepdf = $economic->bookInvoiceInEconomic($row->order_id,$checkOrderStatus);
				if(is_file($bookinvoicepdf))
				{
					$ret = $redshopMail->sendEconomicBookInvoiceMail($row->order_id,$bookinvoicepdf);
				}
			}
		}

		// ORDER MAIL SEND
		if($postdata['task']!="save_without_sendmail")
		{
			$redshopMail->sendOrderMail($row->order_id);
		}
		return $row;
	}

	function sendRegistrationMail($post) {

		$redshopMail = new redshopMail();
		$redshopMail->sendRegistrationMail($post);
	}

	function changeshippingaddress($shippingadd_id, $user_id , $is_company)
	{
		$extra_field = new extra_field();
		$Redconfiguration = new Redconfiguration();
		
		$query = 'SELECT * FROM '.$this->_table_prefix.'users_info '
				.'WHERE address_type like "ST" '
				.'AND user_id="'.$user_id .'" '
				.'AND users_info_id="'.$shippingadd_id.'" ';
		$this->_db->setQuery($query);
		$list = $this->_db->loadObject();
		if(count($list)>0)
		{
			$shipping = $list;
		} else {
			$shipping=$this->setShipping();
		}
		$allowCustomer = '';
		$allowCompany = '';
		if($is_company)
		{
			$allowCustomer = 'style="display:none;"';
		} else {
			$allowCompany = 'style="display:none;"';
		}
		$lists['shipping_customer_field'] = $extra_field->list_all_field(14, $shipping->users_info_id); /// field_section 7 :Customer Address
		$lists['shipping_company_field'] = $extra_field->list_all_field(15, $shipping->users_info_id); /// field_section 8 :Company Address
		$countryarray = $Redconfiguration->getCountryList((array)$shipping,"country_code_ST","ST");
		$shipping->country_code_ST = $shipping->country_code = $countryarray['country_code_ST'];
		$lists['country_code_ST'] = $countryarray['country_dropdown'];
		$statearray = $Redconfiguration->getStateList((array)$shipping,"state_code_ST","country_code_ST","ST",1);
		$lists['state_code_ST'] = $statearray['state_dropdown'];
		
		$htmlshipping= '<table class="adminlist" border="0" width="100%">';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'FIRSTNAME' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="firstname_ST" maxlength="250" value="'.$shipping->firstname.'" /></td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'LASTNAME' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="lastname_ST" maxlength="250" value="'.$shipping->lastname.'" /></td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'ADDRESS' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="address_ST" maxlength="250" value="'.$shipping->address.'" /></td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'ZIP' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="zipcode_ST" maxlength="250" value="'.$shipping->zipcode.'" /></td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'CITY' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="city_ST" maxlength="250" value="'.$shipping->city.'" /></td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'COUNTRY' ).':</td>';
		$htmlshipping .= '<td>'.$lists['country_code_ST'].'</td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'STATE' ).':</td>';
		$htmlshipping .= '<td>'.$lists['state_code_ST'].'</td></tr>';
		$htmlshipping .= '<tr><td width="100" align="right">'.JText::_( 'PHONE' ).':</td>';
		$htmlshipping .= '<td><input class="inputbox" type="text" name="phone_ST" maxlength="250" value="'.$shipping->phone.'" /></td></tr>';
		$htmlshipping .= '<tr><td colspan="2"><div id="exCustomerFieldST" '.$allowCustomer.'>'.$lists['shipping_customer_field'].'</div>
							<div id="exCompanyFieldST" '.$allowCompany.'>'.$lists['shipping_company_field'].'</div></td></tr>';
		$htmlshipping .= '</table>';
		
		return $htmlshipping;
	}
}
?>